package javango.conf;

import java.util.List;
import java.util.StringTokenizer;

import javango.core.Url;
import javango.core.Urls;
import javango.core.contextprocessors.ContextProcessor;
import javango.middleware.Middleware;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author johns
 *
 */
public class TextSettings extends BaseSettings implements Settings {

	private final static Log log = LogFactory.getLog(Settings.class);
	
	@Override
	public List<ContextProcessor> getContextProcessorList() {
		if (contextProcessorList.isEmpty()) {
			String requestContextClasses = get("TemplateContextProcessors");			
			if (requestContextClasses != null) {
				StringTokenizer tokenizer = new StringTokenizer(requestContextClasses, ";");
				while (tokenizer.hasMoreElements()) {
					String s = (String)tokenizer.nextElement();
					try {
						contextProcessorList.add((ContextProcessor)injector.getInstance(Class.forName(s.trim())));
					} catch (ClassNotFoundException e) {
						log.error("Unable to find context processor " + e,e);
					}
				}
			}
		}
		return super.getContextProcessorList();
	}

	@Override
	public List<Middleware> getMiddlewareList() {
		if (middlewareList.isEmpty()) {
			String middlewareClasses = get("Middleware", "javango.middleware.CommonMiddleware");			
			if (middlewareClasses != null) {
				StringTokenizer tokenizer = new StringTokenizer(middlewareClasses, ";");
				while (tokenizer.hasMoreElements()) {
					String s = (String)tokenizer.nextElement();
					try {
						middlewareList.add((Middleware)injector.getInstance(Class.forName(s.trim())));
					} catch (ClassNotFoundException e) {
						log.error("Unable to find middleware " + e,e);
					}
				}
			}
		}
		return super.getMiddlewareList();
	}

	@Override
	public List<Url> getUrlList() {
		if (urlList.isEmpty()) {
			String urlClass = get("Urls", "javango.core.BaseUrls");
			try {
				Urls urls = (Urls)injector.getInstance(Class.forName(urlClass));
				urlList.addAll(urls.getUrlPatterns());
			} catch (ClassNotFoundException e) {
				log.error("Unable to find urls " + e,e);
			}
		}
		return super.getUrlList();
	}

}
